﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.Utility;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCust_BusNatureDAL:Iapp_tblCust_BusNature
    {
        HKEA_CMSEntities model;
        app_tblCust_BusNature busnature;
        private app_tblCust_BusNatureDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCust_BusNatureDAL GetInstance()
        {
            return new app_tblCust_BusNatureDAL();
        }

        public int InsertENT(app_tblCust_BusNature ent) {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCust_BusNature.AddObject(ent);
            model.SaveChanges();
            ent = model.app_tblCust_BusNature.Where(a => a.AppCust_BusNatureUID > 0).OrderBy(a => a.AppCust_BusNatureUID).ToList().Last();
            return Convert.ToInt32(ent.AppCust_BusNatureUID);
        }

        public int SaveList(List<int> toAddList, List<int> toDelList, int customeruid, string LastModifyBy) {
            foreach (var temp in toAddList) {
                var tempValue = model.app_tblCust_BusNature.Where(a=>a.CustomerUID==customeruid).Where(a=>a.BusNatureUID==temp).FirstOrDefault();
                var ent = new app_tblCust_BusNature();
                if (tempValue != null)
                {
                    ent.BusNatureUID = temp;
                    ent.CustomerUID = customeruid;
                    ent.Deleted = 0;
                    ent.LastModifyBy = LastModifyBy;
                    ent.LastModifyDate = DateTime.Now;
                    model.app_tblCust_BusNature.AddObject(ent);
                }
                else {
                    if (tempValue.Deleted == 1) {
                        tempValue.Deleted = 0;
                        tempValue.LastModifyDate = DateTime.Now;
                        tempValue.LastModifyBy = LastModifyBy;
                    }
                }
            }
            foreach (var temp in toDelList) {
                var result = model.app_tblCust_BusNature.Where(a => a.CustomerUID == customeruid).Where(a => a.BusNatureUID == temp).Where(a=>a.Deleted==0).FirstOrDefault();
                if (result != null)
                {
                    result.LastModifyDate = DateTime.Now;
                    result.LastModifyBy = LastModifyBy;
                    result.Deleted = 1;
                }
            }
            return model.SaveChanges();
        }

        public int InsertENT(List<int> BusNatureUIDList, int CustomerUID, string LastModifyBy) {
            return 0;
        }

        public List<app_tblCust_BusNature> GetListByENT(app_tblCust_BusNature ent, PageParam param, string strPropertyName, string strOrderBy)
        {
            var result = model.app_tblCust_BusNature.Where(a => a.AppCust_BusNatureUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.CustomerUID > 0) {
                result = result.Where(a=>a.CustomerUID==ent.CustomerUID);
            }
            if (ent.Cust_BusNatureUID > 0) {
                result = result.Where(a=>a.Cust_BusNatureUID==ent.Cust_BusNatureUID);
            }
            if (!string.IsNullOrEmpty(ent.LastModifyBy)) {
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            }
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName+" "+ strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);//.OrderBy(strPropertyName + " " + strOrderBy)
            return result.ToList();
        }

        public List<app_tblCust_BusNature> GetListByENT(app_tblCust_BusNature ent, bool NeedDelete)
        {
            var result = model.app_tblCust_BusNature.Where(a => a.AppCust_BusNatureUID > 0);
            if (NeedDelete)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Cust_BusNatureUID > 0)
                result = result.Where(a => a.Cust_BusNatureUID == ent.Cust_BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(app_tblCust_BusNature ent, bool NeedDelte)
        {
            var result = model.app_tblCust_BusNature.Where(a=>a.AppCust_BusNatureUID>0);
            if (NeedDelte)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Cust_BusNatureUID > 0)
                result = result.Where(a => a.Cust_BusNatureUID == ent.Cust_BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(app_tblCust_BusNature ent){
            busnature =model.app_tblCust_BusNature.Where(a=>a.AppCust_BusNatureUID==ent.AppCust_BusNatureUID).FirstOrDefault();
            if(busnature!=null){
                busnature.Deleted=ent.Deleted;
                busnature.LastModifyBy=ent.LastModifyBy;
                busnature.LastModifyDate=DateTime.Now;
                busnature.BusNatureUID=ent.BusNatureUID;
                busnature.Cust_BusNatureUID=ent.Cust_BusNatureUID;
                busnature.Remark=ent.Remark;
            }
            return model.SaveChanges();
        }
        public int DeleteENT(app_tblCust_BusNature ent)
        {
            var temp = model.app_tblCust_BusNature.Where(a => a.AppCust_BusNatureUID > 0);
            if (temp.Count() > 0)
            {
                temp = temp.Where(a => a.BusNatureUID == ent.BusNatureUID && a.CustomerUID == ent.CustomerUID);
                var list = temp.ToList();
                for (int i = 0; i < list.Count(); i++)
                {
                    model.app_tblCust_BusNature.DeleteObject(list[i]);
                }
            }
            return model.SaveChanges();
        }

        public List<int> GetCustomerUIDList()
        {
            return model.app_tblCust_BusNature.Where(a => a.CustomerUID > 0).Select(a => a.CustomerUID).ToList();
        }
    } 
}
